package com.funwell.server.clients;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 *
 */
@Component
public class MyJPushClient {

    @Autowired
    private JPushClient client;

    /**
     * 使用别名进行通知推送
     * @param aliases 需要推送的别名列表
     * @param content 推送内容
     */
    public void pushNotification(Collection<String> aliases,String content){
        PushPayload payload= PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliases))
                .setNotification(Notification.alert(content))
                .build();
        try {
            PushResult  result=client.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用别名进行通知定时推送（单次推送）
     * @param aliases 需要推送的别名列表
     * @param content 推送内容
     * @param schedule 推送时间
     * @param name 推送名称
     * @return  返回定时推送ID
     */
    public String pushNotificationSchedule(Collection<String> aliases,String content,String schedule,String name){
        PushPayload payload= PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliases))
                .setNotification(Notification.alert(content))
                .build();
        try {
            ScheduleResult result=client.createSingleSchedule(name,schedule,payload);
            return result.getSchedule_id();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 使用别名进行自定消息推送
     * @param aliases 需要推送的别名列表
     * @param title 标题
     * @param content 推送内容
     */
    public void pushMessage(Collection<String> aliases,String title,String content){
        PushPayload payload= PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliases))
                .setMessage(Message.newBuilder().setMsgContent(content).setTitle(title).build())
                .build();
        try {
            PushResult  result=client.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }

    }

    /**
     * 使用别名进行自定消息定时推送（单次推送）
     * @param aliases 需要推送的别名列表
     * @param title 标题
     * @param content 推送内容
     * @return  返回定时推送ID
     */
    public String  pushMessageSchedule(Collection<String> aliases,String title,String content,String schedule,String name){
        PushPayload payload= PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliases))
                .setMessage(Message.newBuilder().setMsgContent(content).setTitle(title).build())
                .build();
        try {
            ScheduleResult result=client.createSingleSchedule(name,schedule,payload);
            return result.getSchedule_id();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 根据定时推送ID删除定时推送任务
     * @param scheduleId 定时推送ID
     */
    public void deleteSchedule(String scheduleId){
        try {
            client.deleteSchedule(scheduleId);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }








}
